package fhp.hlhj.giantfold.activity.camera

import android.app.Activity
import android.content.Intent
import android.graphics.Point
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.event.CapImgEvent
import kotlinx.android.synthetic.main.activity_camera_aty.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.File

class CameraAty : BaseActivity(), CircleCameraInterface.CamOpenOverCallback {
    override fun cameraHasOpened() {
        val holder = surfaceview.getSurfaceHolder()
        CircleCameraInterface.getInstance(this@CameraAty).doStartPreView(holder, previewRate)
    }

    private val TAG = "CircleCameraInterface"
    internal var previewRate = -1f
    var circlePictureSize: Point?=null
    var screenW :Int = 0
    private val TAKEPHOTO = 1
    var screenH :Int = 0
    override fun getContentId(): Int {
        return R.layout.activity_camera_aty
    }

    override fun initView() {
        screenW= resources.displayMetrics.widthPixels
        screenH=resources.displayMetrics.heightPixels
        Log.i(TAG, "获取屏幕宽度 =  " + screenW)
        initCamera()
        startCamera()
        initListtener()
    }

    override fun onDestroy() {
        super.onDestroy()
        CircleCameraInterface.getInstance(this).destory()
    }
    private fun initListtener() {
        btTakePhoto.setOnClickListener {
/*            if (circlePictureSize == null) {
                circlePictureSize = CreateCirclePicture(
                        DisplayUtil.dip2px(this@CameraAty, screenW),
                        DisplayUtil.dip2px(this@CameraAty, screenH))
            }*/
            CircleCameraInterface.getInstance(this@CameraAty).doTakePicture(screenW, screenH)
        }
    }

    private fun initCamera() {
        val params = surfaceview.layoutParams
        val p = DisplayUtil.getScreenMetrics(this)
        params.width = p.x
        params.height = p.y
        previewRate = DisplayUtil.getScreenRate(this)//默认全屏的比例预览
        surfaceview.layoutParams = params
    }

    private fun startCamera() {
        val openThread = object : Thread() {
            override fun run() {
                CircleCameraInterface.getInstance(this@CameraAty).doOpenCamera(this@CameraAty)
            }
        }
        openThread.start()
    }
    private fun CreateCirclePicture(w: Int, h: Int): Point {
        val wScreen = DisplayUtil.getScreenMetrics(this).x
        val hScreen = DisplayUtil.getScreenMetrics(this).y
        val wSavePicture = CircleCameraInterface.getInstance(this@CameraAty).doGetPrictureSize().y//因为图片旋转了，所以宽高换位
        val hSavePicture = CircleCameraInterface.getInstance(this@CameraAty).doGetPrictureSize().x
        val wRate = wSavePicture.toFloat() / wScreen.toFloat()
        val hRate = hSavePicture.toFloat() / hScreen.toFloat()
        val rate = if (wRate <= hRate) wRate else hRate//也可以按照最小比率计算
        val wRectPicture = (w * wRate).toInt()
        val hRectPicture = (h * hRate).toInt()
        return Point(wRectPicture, hRectPicture)
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public fun handImg(event:CapImgEvent){
        if (event.path != null) {
            val file = File(event.path)
            val uri = Uri.fromFile(file)
            val intent1 = Intent(this@CameraAty, ImageCapterActivity::class.java)
            intent1.data = uri
            intent1.putExtra("thumbPath", event.path)
            startActivityForResult(intent1, TAKEPHOTO)
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode== Activity.RESULT_OK){
            when(requestCode){
                TAKEPHOTO->{
                    if (data != null) {
                        val thumbPath = data.getStringExtra("thumbPath")
                        val intent = Intent()
                        intent.putExtra("thumbPath", thumbPath)
                        setResult(Activity.RESULT_OK, intent)
                    }
                    finish()
                }
            }
        }
    }
}
